package com.jxd.petMgr.servlet;

import com.jxd.petMgr.model.Pet;
import com.jxd.petMgr.model.PetOwner;
import com.jxd.petMgr.service.IPetOwnerService;
import com.jxd.petMgr.service.IPetService;
import com.jxd.petMgr.service.IPetStoreService;
import com.jxd.petMgr.service.impl.PetOwnerServiceImpl;
import com.jxd.petMgr.service.impl.PetServiceImpl;
import com.jxd.petMgr.service.impl.PetStoreServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/sellServlet")
public class SellServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        IPetService petService = new PetServiceImpl();
        IPetStoreService petStoreService = new PetStoreServiceImpl();
        IPetOwnerService petOwnerService = new PetOwnerServiceImpl();
        String msg = "";
//        int totalPrice = 0;//宠物总价格
        //接收前台数据
        String str = request.getParameter("petIds");
        String strPetStoreId = request.getParameter("petStoreId");
        int petStoreId = Integer.parseInt(strPetStoreId);
        //把字符串分隔为数组
        String []  strPets = str.split(",");
        //转换为int 类型的集合
        List<Integer> petIdList = new ArrayList<>();
        //宠物集合
        List<Pet> petList = new ArrayList<>();
        for (String strPet : strPets) {
            if (strPet != null && !"".equals(strPet)) {
                int id = Integer.parseInt(strPet);
//                totalPrice += petService.getPetById(id).getPrice();
                petIdList.add(id);
            }
        }
        for (int petId:petIdList) {
            Pet pet = petService.getPetById(petId);
            pet.setStore_id(petStoreId);
            petList.add(pet);
            //是否购买成功
//            boolean isSucc = petOwnerService.sell(pet) && petStoreService.buy(pet);
//            isAllSucc.add(isSucc);
        }
//        for (Boolean aBoolean : isAllSucc) {
//            if (!aBoolean) {
//                //有一个宠物卖出失败
//                msg = "卖出失败";
//                break;
//            }
//        }
//        if (!"卖出失败".equals(msg)){
//            msg = "全部卖出成功";
//        }
        if (petOwnerService.sellBatch(petList) && petStoreService.sellBatch(petList)){
            msg = "卖出成功";
        } else {
            msg = "卖出失败";
        }
        PrintWriter pw = response.getWriter();
        pw.write(msg);
        pw.flush();
        pw.close();
//        request.setAttribute("msg",msg);
//        request.getRequestDispatcher("/toMyAccountServlet").forward(request,response);
    }
}
